package com.schoolpro.service.impl;


import com.schoolpro.exception.PropertyException;
import com.schoolpro.mapper.CategoryMapper;
import com.schoolpro.mapper.CategoryPropertyMapper;
import com.schoolpro.mapper.ValueMapper;
import com.schoolpro.pojo.DO.CategoryDO;
import com.schoolpro.pojo.DO.PropertyDO;
import com.schoolpro.pojo.DO.ValueDO;
import com.schoolpro.pojo.DTO.PropertyAddDTO;
import com.schoolpro.pojo.DTO.PropertyDTO;
import com.schoolpro.pojo.DTO.PropertyUpdateDTO;
import com.schoolpro.pojo.DTO.ValueDTO;
import com.schoolpro.service.PropertyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
* @author : Hinton
* @时间 : 2月 05 00:13
*/


@Service
@Slf4j
public class PropertyServiceImpl implements PropertyService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private CategoryPropertyMapper propertyMapper;
    @Autowired
    private ValueMapper valueMapper;

    @Override
    @Transactional
    public void addPropertyBatch(PropertyAddDTO propertyDTOList) {
        // 查询对应类目
        CategoryDO categoryDO = categoryMapper.getById(propertyDTOList.getCategoryId());
        if (categoryDO == null) {
            throw new PropertyException("类目[" + propertyDTOList.getCategoryId() + "不存在");
        }
        List<PropertyDO> propertyDOList = new ArrayList<>();

        // 检索每一个属性
        for (PropertyDTO propertyDTO : propertyDTOList.getPropertyDTOList()) {
            PropertyDO propertyDO = new PropertyDO();
            BeanUtils.copyProperties(propertyDTO, propertyDO);
            propertyDO.setGmtCreate(LocalDateTime.now());
            propertyDO.setGmtModified(LocalDateTime.now());
            propertyDO.setCategoryName(categoryDO.getName());

            // 可枚举 - 插入value 枚举值
            if (propertyDO.getEnumerable() == 1) {
                ValueDO valueDO = new ValueDO();
                List<ValueDO> valueDOList = new ArrayList<>();
                for (ValueDTO valueDTO : propertyDTO.getEnumValues()) {
                    valueDO.setGmtCreate(LocalDateTime.now());
                    valueDO.setGmtModified(LocalDateTime.now());
                    valueDO.setCategoryName(categoryDO.getName());
                    valueDO.setPropertyName(propertyDTO.getName());
                    valueDO.setFieldValue(valueDTO.getFieldValue());
                    valueDO.setComments(valueDTO.getComments());

                    valueDOList.add(valueDO);
                }

                // 批量插入value
                valueMapper.insertBatch(valueDOList);
            } else if (propertyDO.getEnumerable() == 0) {
                // 没有枚举值, 但如果长度 != 0
                if (!propertyDTO.getEnumValues().isEmpty()) {
                    throw new PropertyException("不应有枚举值");
                }
            }

            propertyDOList.add(propertyDO);
        }

        propertyMapper.insertBatch(propertyDOList);

    }

    /**
     * 删除属性
     * @param ids
     */
    @Override
    public void deleteProperty(List<Integer> ids) {
        propertyMapper.deleteProperty(ids);
    }

    /**
     * 修改属性
     * @param propertyUpdateDTO
     */
    @Override
    @Transactional
    public void updateProperty(PropertyUpdateDTO propertyUpdateDTO) {
        // 1. 获得类目
        CategoryDO categoryDO = categoryMapper.getById(propertyUpdateDTO.getCategoryId());
        if (categoryDO == null) {
            throw new PropertyException("类目[" + propertyUpdateDTO.getCategoryId() + "不存在");
        }

        // 2. 删除旧的属性
        propertyMapper.deleteByCategoryName(categoryDO.getName());
        valueMapper.deleteByCategoryName(categoryDO.getName());



        // 3. 插入新的属性
        List<PropertyDO> propertyDOList = new ArrayList<>();
        for (PropertyDTO propertyDTO : propertyUpdateDTO.getPropertyDTOList()) {
            PropertyDO propertyDO = new PropertyDO();
            BeanUtils.copyProperties(propertyDTO, propertyDO);
            propertyDO.setGmtCreate(LocalDateTime.now());
            propertyDO.setGmtModified(LocalDateTime.now());
            propertyDO.setCategoryName(categoryDO.getName());

            // 可枚举 - 插入value 枚举值
            if (propertyDO.getEnumerable() == 1) {
                List<ValueDO> valueDOList = new ArrayList<>();
                for (ValueDTO valueDTO : propertyDTO.getEnumValues()) {
                    ValueDO valueDO = new ValueDO();

                    valueDO.setGmtCreate(LocalDateTime.now());
                    valueDO.setGmtModified(LocalDateTime.now());
                    valueDO.setCategoryName(categoryDO.getName());
                    valueDO.setPropertyName(propertyDTO.getName());
                    valueDO.setFieldValue(valueDTO.getFieldValue());
                    valueDO.setComments(valueDTO.getComments());

                    valueDOList.add(valueDO);
                }

                // 批量插入value
                valueMapper.insertBatch(valueDOList);
            } else if (propertyDO.getEnumerable() == 0) {
                // 没有枚举值, 但如果长度 != 0
                if (!propertyDTO.getEnumValues().isEmpty()) {
                    throw new PropertyException("不应有枚举值");
                }
            }

            propertyDOList.add(propertyDO);
        }

        propertyMapper.insertBatch(propertyDOList);



    }
}
